﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.BusinessLogic.Advertisement;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;

namespace Web.Cms.Advertistment
{
    public partial class addEditBanner : AdminPage
    {
        int advId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Directory.Exists(Server.MapPath("/Uploads/Banner/" + CurrentUser.UserID)))
                Directory.CreateDirectory(Server.MapPath("/Uploads/Banner/" + CurrentUser.UserID));
            fpMedia.fpUploadDir = "/Uploads/Banner/" + CurrentUser.UserID;
            advId = ObjConvertUtility.ToInt32(Request.QueryString["advid"]);
            if (!IsPostBack)
            {
                BannerPos.Fill(dropPriority);
                ObjMiscUtility.FillIndex(dropPriority, 1, 100, 0);
                dropGroupBanner.DataSource = AdvertisementPositionsBL.SelectAll();
                dropGroupBanner.DataTextField = "Title";
                dropGroupBanner.DataValueField = "PositionID";
                dropGroupBanner.DataBind();
                AdvertisementInfo advertisementInfo = AdvertisementBL.Select(advId);
                if (advertisementInfo != null && advId > 0)
                {
                    txtID.Text = advertisementInfo.AdvertisementID.ToString();
                    txtName.Text = advertisementInfo.Title;
                    txtDescription.Text = advertisementInfo.Description;
                    txtLink.Text = advertisementInfo.Target;
                    fpMedia.Text = advertisementInfo.MediaSrc;
                    TxtCustomer.Text = advertisementInfo.CustomerID.ToString();
                    rcldStartDate.SelectedDate = advertisementInfo.StartDate;
                    rcldEndDate.SelectedDate = advertisementInfo.EndDate;
                    txtClick.Text = advertisementInfo.Click.ToString();
                    dropGroupBanner.SelectedValue = advertisementInfo.PositionID.ToString();
                    if (advertisementInfo.IsEnable == 1)
                    {
                        chkEnable.Checked = true;
                    }
                    else
                    {
                        chkEnable.Checked = false;
                    }
                    txtViewCount.Text = advertisementInfo.ViewCount.ToString();
                    litMedia.Visible = true;
                    dropMediaType.SelectedValue = advertisementInfo.MediaType.ToString();
                    MiscUtility.SetSelected(dropPriority, advertisementInfo.Priority.ToString());
                    lblUpdateStatus.Visible = false;
                    btnAdd.Visible = false;
                    btnUpdate.Visible = true;
                }
                else
                {
                    btnAdd.Visible = true;
                    btnUpdate.Visible = false;
                }
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AdvertisementInfo adv = new AdvertisementInfo();
                if (adv == null) return;

                if (DateTime.Compare(rcldEndDate.SelectedDate, rcldStartDate.SelectedDate) < 0)
                {
                    trShowWarning.Visible = true;
                    rcldEndDate.Focus();
                    lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
                    return;
                }
                else
                {
                    trShowWarning.Visible = false;
                }
                FillData(adv);
                int advId = AdvertisementBL.Insert(adv);
               
                lblUpdateStatus.Text = "Thêm mới thành công";

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                AdvertisementInfo adv = new AdvertisementInfo();
                FillData(adv);
                AdvertisementBL.Update(adv);               
                lblUpdateStatus.Text = "Cập nhật thành công";
                lblUpdateStatus.Visible = true;
            }
            catch (Exception ex)
            {

                lblUpdateStatus.Text = ex.Message;
            }
        }

        private void FillData(AdvertisementInfo adv)
        {
            adv.AdvertisementID = advId;
            adv.Title = txtName.Text;
            adv.Description = txtDescription.Text;
            adv.MediaSrc = fpMedia.Text;
            adv.Width = 0;
            adv.Height = 0;
            adv.MediaType = ConvertUtility.ToInt32(dropMediaType.SelectedValue);
            adv.Target = txtLink.Text;
            adv.Priority = ConvertUtility.ToInt32(dropPriority.SelectedValue);
            adv.CreatedBy = CurrentUser.UserID;
            adv.CreatedOn = DateTime.Now;
            adv.StartDate = ConvertUtility.ToDateTime(rcldStartDate.SelectedDate);
            adv.EndDate = ConvertUtility.ToDateTime(rcldEndDate.SelectedDate);
            adv.CustomerID = 0;
            adv.Click = 0;
            adv.ViewCount = 0;
            if (chkEnable.Checked)
            {
                adv.IsEnable = 1;
            }
            else
            {
                adv.IsEnable = 0;
            }
            adv.PositionID = Convert.ToInt32(dropGroupBanner.SelectedValue);
        }
    }
}